<template>
  <div class="teacher-list">
    <StudentNavbar title="老师列表" />
    
    <!-- 搜索条件 -->
    <el-card class="search-card">
      <template #header>
        <div class="card-header">
          <span>搜索条件</span>
        </div>
      </template>
      <el-form :model="searchForm" label-width="80px" inline>
        <el-form-item label="用户名">
          <el-input v-model="searchForm.username" placeholder="请输入用户名" clearable></el-input>
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="searchForm.email" placeholder="请输入邮箱" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="searchTeachers" icon="el-icon-search">搜索</el-button>
          <el-button @click="resetSearch" icon="el-icon-refresh">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    
    <!-- 老师列表 -->
    <div class="teacher-grid">
      <div 
        class="teacher-card" 
        v-for="teacher in teacherList" 
        :key="teacher.id"
        :class="{ 'selected': chosenTeacherIds.includes(teacher.id) }"
      >
        <div class="teacher-avatar">
          <i class="el-icon-user-solid"></i>
        </div>
        <div class="teacher-info">
          <h3 class="teacher-name">{{ teacher.username }}</h3>
          <p class="teacher-email">{{ teacher.email }}</p>
        </div>
        <div class="teacher-action">
          <el-button 
            v-if="!chosenTeacherIds.includes(teacher.id)" 
            type="primary" 
            round
            @click="chooseTeacher(teacher.id)"
            icon="el-icon-plus">
            选择
          </el-button>
          <el-tag v-else type="success" effect="dark">已选择</el-tag>
        </div>
      </div>
      
      <!-- 空状态 -->
      <el-empty v-if="teacherList.length === 0 && !loading" description="暂无老师数据"></el-empty>
    </div>
    
    <!-- 加载状态 -->
    <div class="loading-container" v-if="loading">
      <i class="el-icon-loading"></i>
      <span>加载中...</span>
    </div>
  </div>
</template>

<script>
import StudentAPI from '@/api/student'
import StudentNavbar from '@/components/StudentNavbar.vue'

export default {
  name: 'TeacherList',
  components: {
    StudentNavbar
  },
  data() {
    return {
      searchForm: {
        username: '',
        email: ''
      },
      teacherList: [],
      chosenTeacherIds: [], // 已选择的老师ID列表
      loading: false
    }
  },
  created() {
    this.fetchTeacherList()
    this.fetchChosenTeacherIds()
  },
  methods: {
    // 获取老师列表
    async fetchTeacherList() {
      this.loading = true
      try {
        // 构造查询参数对象
        const queryParams = {
          username: this.searchForm.username,
          email: this.searchForm.email
        }
        
        const res = await StudentAPI.getTeacherList(queryParams)
        if (res.data.code === 200 && res.data.msg === 'success') {
          this.teacherList = res.data.data || []
        } else {
          this.$message.error(res.data.message || '获取老师列表失败')
        }
      } catch (error) {
        console.error('获取老师列表失败:', error)
        this.$message.error('获取老师列表失败: ' + (error.response?.data?.message || error.message))
      } finally {
        this.loading = false
      }
    },

    // 获取已选择的老师ID列表
    async fetchChosenTeacherIds() {
      try {
        const res = await StudentAPI.getChosenTeacherIds()
        if (res.data.code === 200 && res.data.msg === 'success') {
          this.chosenTeacherIds = res.data.data || []
        } else {
          this.$message.error(res.data.message || '获取已选择老师列表失败')
        }
      } catch (error) {
        console.error('获取已选择老师列表失败:', error)
        this.$message.error('获取已选择老师列表失败: ' + (error.response?.data?.message || error.message))
      }
    },

    // 搜索老师
    searchTeachers() {
      this.fetchTeacherList()
    },

    // 重置搜索条件
    resetSearch() {
      this.searchForm.username = ''
      this.searchForm.email = ''
      this.fetchTeacherList()
    },

    // 选择老师
    async chooseTeacher(teacherId) {
      try {
        const res = await StudentAPI.chooseTeacher(teacherId)
        if (res.data.code === 200 && res.data.msg === 'success') {
          this.$message.success('选择老师成功')
          // 更新已选择的老师列表
          await this.fetchChosenTeacherIds()
        } else {
          this.$message.error(res.data.message || '选择老师失败')
        }
      } catch (error) {
        console.error('选择老师失败:', error)
        this.$message.error('选择老师失败: ' + (error.response?.data?.message || error.message))
      }
    }
  }
}
</script>

<style scoped>
.teacher-list {
  padding: 20px;
  background: linear-gradient(135deg, #1e3c72 0%, #2a5298 100%);
  min-height: calc(100vh - 40px);
}

.search-card {
  background: rgba(255, 255, 255, 0.08);
  backdrop-filter: blur(10px);
  border-radius: 15px;
  border: 1px solid rgba(255, 255, 255, 0.1);
  margin-bottom: 30px;
  color: white;
}

.card-header {
  font-size: 1.2rem;
  font-weight: 600;
}

.teacher-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 20px;
  margin-top: 20px;
}

.teacher-card {
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
  border-radius: 15px;
  padding: 20px;
  display: flex;
  align-items: center;
  transition: all 0.3s ease;
  border: 1px solid rgba(255, 255, 255, 0.1);
  color: white;
}

.teacher-card:hover {
  transform: translateY(-5px);
  background: rgba(255, 255, 255, 0.15);
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.2);
}

.teacher-card.selected {
  border: 1px solid #409eff;
  box-shadow: 0 0 15px rgba(64, 158, 255, 0.3);
}

.teacher-avatar {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 20px;
  font-size: 24px;
}

.teacher-info {
  flex: 1;
}

.teacher-name {
  margin: 0 0 8px 0;
  font-size: 1.2rem;
  font-weight: 600;
}

.teacher-email {
  margin: 0;
  font-size: 0.9rem;
  opacity: 0.8;
}

.teacher-action {
  display: flex;
  align-items: center;
}

.loading-container {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 40px;
  color: white;
  font-size: 1.2rem;
}

.loading-container i {
  font-size: 2rem;
  margin-right: 10px;
  animation: rotating 2s linear infinite;
}

@keyframes rotating {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}

@media (max-width: 768px) {
  .teacher-grid {
    grid-template-columns: 1fr;
  }
  
  .teacher-card {
    flex-direction: column;
    text-align: center;
  }
  
  .teacher-avatar {
    margin-right: 0;
    margin-bottom: 15px;
  }
  
  .teacher-action {
    margin-top: 15px;
  }
}
</style>